package com.mfoyou.agent.web.controller.others;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mfoyou.agent.web.intercepter.UrlRight;
import com.mfoyou.agent.web.intercepter.UserRight;
import com.mfoyou.agent.web.utils.redis.RedisCacheManger;
import com.mfoyou.agent.web.utils.yinianyun.YinianRet;

@Path("yinianyun/mfoyou")
@Service
public class YinianyunCallBack {

    public static final String PRINTER_PRE = "printer:machine:";

    @Autowired
    RedisCacheManger redisCacheManger;

    @POST
    @Path(value = "callback")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public YinianRet callback(@Context HttpServletRequest request,
                              @Context HttpServletResponse response) throws Exception {
        String cmd = request.getParameter("cmd");
        if (cmd != null && cmd.equals("oauth_printStatus")) {
            String machine_code = request.getParameter("machine_code");
            String online = request.getParameter("online");
            redisCacheManger.hset(PRINTER_PRE + machine_code, "online", online);
            return new YinianRet("OK");
        }
        return new YinianRet("OK");
    }

    @GET
    @Path(value = "callback")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public YinianRet getcallback(@Context HttpServletRequest request,
                                 @Context HttpServletResponse response) throws Exception {
        return new YinianRet("OK");
    }

    /**
     * 新老系统状态同步
     *
     * @param request
     * @return
     */
    @GET
    @Path(value = "mycallback")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public YinianRet mycallback(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                @QueryParam("code") String code, @QueryParam("online") String online) {
        redisCacheManger.hset(PRINTER_PRE + code, "online", online);
        return new YinianRet("OK");
    }
}
